package isanfeng.tuxiaoer.pattern.creational.Esingleton.hungry;

import java.io.Serializable;

/**
 * 饿汉式: 类加载时初始化.
 * @author 李红辉
 * @version 1.0
 * @date 2019/10/9 16:10
 */
public class HungrySingleton implements Serializable {
    private final static HungrySingleton hungrySingleton;
    static {
        hungrySingleton = new HungrySingleton();
    }
    private HungrySingleton() {
        if (hungrySingleton != null) {
            throw new RuntimeException("禁止通过反射创建单例对象");
        }
    }
    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }

    /**
     * 通过反射生成了另一个对象,然后检查是否有该方法,若有则调用该方法...为什么不先判断是否有该方法...
     * @return
     */
    private Object readResolve() {
        return hungrySingleton;
    }
}
